﻿using System;
using System.Collections.Generic;
using System.Data;

namespace IncubatorServices
{
    public class QuizChoice
    {
        public int id;
        public string text;
        public string value;
    }

    public class QuizQuestion
    {
        public int id;
        public string name;
        public string text;
        public List<QuizChoice> choices;
        public string answer; //to be compared to value
        public int value;
    }

    public class QuizStatus
    {
        public Quiz quiz;
        public Company company;
        public List<string> answers;
        public int answeredCorrectly;
        public int totalEarned;
        public QuizStatus() { }
        public void LoadData(Quiz q, Company c)
        {
            quiz = q;
            company = c;
            sqlManager sql = new sqlManager();
            string query = "SELECT completed_questions FROM quiz_status WHERE company_id = '" + c.id + "' AND quiz_id = '" + q.id + "'";
            //answered = Int32.Parse(sql.GetScalar(query));
            query = "SELECT a.question_id, a.answer FROM company_answers a JOIN quiz_question q ON q.id = a.question_id WHERE a.company_id = '" + c.id + "' AND q.quiz_id = '" + q.id + "'";
            DataSet ds = sql.GetDataSet(query);
            answers = new List<string>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                string answer = sqlManager.GetData(r["answer"]);
                int question_id = Int32.Parse(sqlManager.GetData(r["question_id"]));
                foreach (QuizQuestion question in q.questions)
                {
                    if (question.id == question_id)
                    {
                        //we've found our question
                        bool correct = (question.answer == answer);
                        if (correct)
                        {
                            totalEarned += question.value;
                            answeredCorrectly++;
                        }
                    }
                }
                answers.Add(answer);
            }
        }
    }

    public class Quiz
    {
        public int id;
        public string name;
        public int cost;
        public string category;
        public List<QuizQuestion> questions;

        public Quiz()
        {

        }

        public bool AnswerQuestion(Company c, QuizQuestion question, string answer)
        {
            sqlManager sql = new sqlManager();
            string query = "INSERT INTO company_answers (question_id, company_id, answer) VALUES ('" + question.id + "','" + c.id + "','" + answer + "')";
            sql.NonQuery(query);
            //update money if the answer is correct
            if (question.answer == answer)
            {
               // c.money += question.value;
                //c.storeData();
                Share.splitProfits(c, question.value);
            }
            return (question.answer == answer);
        }
        public bool BuyQuiz(Company c)
        {
            sqlManager sql = new sqlManager();
            //TODO: (1) test if user already owns the quiz, (2) test if user has enough money for quiz
            //store the fact that company has bought the quiz
            string query = "INSERT INTO quiz_status (quiz_id, company_id, completed_questions) VALUES ('" + id.ToString() + "','" + c.id + "','0')";
            sql.NonQuery(query);

            //subtract cost of quiz from company's money
            c.money -= cost;
            c.storeData();
            return true;
        }
        public void LoadData(int id_)
        {
            sqlManager sql = new sqlManager();
            string query = "SELECT name, cost, category FROM quiz WHERE id = '" + id_ + "'";

            DataSet ds = sql.GetDataSet(query);
            this.id = id_;
            this.name = sqlManager.GetData(ds.Tables[0].Rows[0]["name"]);
            Int32.TryParse(sqlManager.GetData(ds.Tables[0].Rows[0]["cost"]), out this.cost);
            this.category = sqlManager.GetData(ds.Tables[0].Rows[0]["category"]);

            query = "SELECT id, name, text, answer, value FROM quiz_question WHERE quiz_id = '" + this.id + "'";
            DataSet ds_questions = sql.GetDataSet(query);
            this.questions = new List<QuizQuestion>();
            foreach (DataRow r in ds_questions.Tables[0].Rows)
            {
                QuizQuestion q = new QuizQuestion();
                q.id = (int)r["id"];
                q.name = sqlManager.GetData(r["name"]);
                q.answer = sqlManager.GetData(r["answer"]);
                q.text = sqlManager.GetData(r["text"]);
                q.value = Int32.Parse(sqlManager.GetData(r["value"]));
                query = "SELECT id,text, value FROM quiz_choice WHERE question_id = '" + q.id + "'";
                DataSet ds_choices = sql.GetDataSet(query);
                q.choices = new List<QuizChoice>();
                foreach (DataRow cr in ds_choices.Tables[0].Rows)
                {
                    QuizChoice choice = new QuizChoice();
                    choice.id = (int)cr["id"];
                    choice.text = sqlManager.GetData(cr["text"]);
                    choice.value = sqlManager.GetData(cr["value"]);
                    q.choices.Add(choice);
                }
                this.questions.Add(q);
            }


        }
    }
}